package com.bajiao.service.order.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.bajiao.common.constant._Whole;
import com.bajiao.common.util.UUIDGenerator;
import com.bajiao.entity.order.CouponOrder;
import com.bajiao.entity.order.RefundCouponOrder;
import com.bajiao.entity.order.RefundOrder;
import com.bajiao.mapper.order.RefundCouponOrderMapper;
import com.bajiao.service.order.CouponOrderService;
import com.bajiao.service.order.RefundCouponOrderService;
import com.bajiao.service.user.UserService;
import com.bajiao.vo.order.RefundCouponOrderAdmin;
import com.bajiao.vo.order.RefundOrderAdmin;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * @Description: 优惠券退款单
 * @Author: FanJq
 * @Date:   2024-12-14
 * @Version: V1.0
 */
@Service
public class RefundCouponOrderServiceImpl extends ServiceImpl<RefundCouponOrderMapper, RefundCouponOrder> implements RefundCouponOrderService {

    @Lazy
    @Resource
    private CouponOrderService orderService;
    @Resource
    private UserService userService;

    /**
     * @Description
     * 创建退款单
     * @date 2024/12/20 14:45
     * @author FanJq
     * @param order
     * @param reason
     * @return {@link Boolean}
    */
    @Override
    public Boolean create(CouponOrder order, String reason) {
        RefundCouponOrder model = this.getByOrderNo(order.getOrderNo());
        if (ObjectUtil.isNull(model)) {
            model = new RefundCouponOrder();
        }
        model.setOrderNo(order.getOrderNo());
        model.setRefundOrderNo(UUIDGenerator.getOrderNoByTime(_Whole.refundCouponOrderNoPrefix));
        model.setUserId(order.getUserId());
        model.setCouponId(order.getCouponId());
        model.setPayPrice(order.getPayPrice());
        model.setRefundPrice(order.getPayPrice());
        model.setStatus(_Whole.ZERO);
        model.setReason(reason);
        return super.saveOrUpdate(model);
    }

    /**
     * @Description
     * 根据主订单编号获取
     * @date 2024/12/20 15:07
     * @author FanJq
     * @param orderNo
     * @return {@link RefundCouponOrder}
    */
    @Override
    public RefundCouponOrder getByOrderNo(String orderNo) {
        return super.lambdaQuery().eq(RefundCouponOrder::getOrderNo, orderNo).one();
    }

    /**
     * @Description
     * 退款单审批
     * @date 2024/12/20 15:08
     * @author FanJq
     * @param refundOrder
     * @param status
     * @return
    */
    @Override
    public void approve(RefundCouponOrder refundOrder, int status) {
        if (refundOrder.getStatus() != _Whole.ZERO)
            return;
        //@create by FanJq 2024/12/20 15:10 先更新退款单状态
        refundOrder.setStatus(status);
        super.updateById(refundOrder);
        //@create by FanJq 2024/12/20 15:10 订单执行退款逻辑
        orderService.refundLogic(refundOrder, status);
        if (status == _Whole.ONE && refundOrder.getRefundPrice().signum() > _Whole.ZERO) {
            //@create by FanJq 2024/12/20 15:10 退款金额入账
            userService.changeMoney(refundOrder.getUserId(), refundOrder.getPayPrice(), _Whole.ONE, refundOrder.getRefundOrderNo(), "优惠券订单退款", _Whole.FOUR);
        }
    }

    /**
     * @Description
     * 管理后台列表
     * @date 2025/1/8 17:23
     * @author FanJq
     * @param orderNo
     * @param keywords
     * @param title
     * @param status
     * @param createTimeBegin
     * @param createTimeEnd
     * @return {@link List< RefundCouponOrderAdmin>}
    */
    @Override
    public List<RefundCouponOrderAdmin> getAdminList(String orderNo, String keywords, String title, Integer status, String createTimeBegin, String createTimeEnd) {
        if (StrUtil.isAllNotBlank(createTimeBegin, createTimeEnd))
            createTimeEnd = createTimeEnd + " 23:59:59";
        List<RefundCouponOrderAdmin> resultList = super.baseMapper.getAdminList(orderNo, keywords, title, status, createTimeBegin, createTimeEnd);
        return resultList;
    }
}
